package com.rbtnet.ssc.banker.provider;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rbtnet.ssc.banker.Banker;
import com.rbtnet.ssc.utils.HttpUtils;
import javazoom.jl.player.Player;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author 1136009436@qq.com
 */
public class B365Provider implements Banker {
	
    HttpClient client = HttpClients.createDefault();

    HttpUtils httpUtils = new HttpUtils();

    public boolean login(String username, String password) {
        try {

            httpUtils.get("http://999.cc88866.cc/Mobile_Login#login");

            //获取验证码
            getCode();

            //提醒用户并输入验证码
            System.out.println("请输入验证码");
            String code;
            Scanner in = new Scanner(System.in);
            code = in.nextLine();

            in.close();

            Map<String, String> params = new HashMap<String, String>();
            params.put("login", username);
            params.put("pass", password);
            params.put("authnum", code);

            httpUtils.post("http://999.cc88866.cc/Mobile_Login/Login", params);

            System.out.println("登录成功");

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }

    public boolean getCode() {
        try {
            httpUtils.download("http://999.cc88866.cc/Mobile_Login/authnum", new FileOutputStream(new File("verifyCode.png")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public boolean doBet(String lottyid,String data,String adv) {
        String url = "http://999.cc88866.cc/DoBets/index/37";

        Map<String, String> params = new HashMap<String, String>();
        params.put("lottyid", lottyid);
        params.put("data", data);
        params.put("adv", adv);

        try {
            httpUtils.post(url, params);
            System.out.println("下单成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String getAdv() {
        try {
            String entity = httpUtils.get("http://999.cc88866.cc/DoBets/__Advisory");
            Object data = JSONObject.parseObject(entity).get("data");
            String adv = JSONObject.parseObject(data.toString()).get("adv").toString();
            return adv;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getCountdown() {
        try {
            String entity = httpUtils.get("http://999.cc88866.cc/LobbyOp/getCountdown/37");

            Object data = JSONObject.parseObject(entity).get("data");
            Object now = JSONObject.parseObject(data.toString()).get("now");
            //Object next = JSONObject.parseObject(data.toString()).get("next");
            String lottyid = JSONObject.parseObject(now.toString()).get("lottyid").toString();
            String round = JSONObject.parseObject(now.toString()).get("round").toString();
            System.out.println("第"+round+"期");
            return lottyid;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Double balance() {
        try {
            String entity = httpUtils.get("http://999.cc88866.cc/Common/getUserBalance");

            Object data = JSONObject.parseObject(entity).get("data");
            String balance = JSONObject.parseObject(data.toString()).get("balance").toString();
            /*if(Double.parseDouble(balance) < 10){
                play("dataDancing.mp3");
                System.exit(1);
            }*/
            return Double.parseDouble(balance);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 播放声音
     * @param fileName
     */
    public static void play(String fileName) {
        try {
            BufferedInputStream buffer = new BufferedInputStream(
                    new FileInputStream(fileName));
            Player player = new Player(buffer);
            player.play();
        } catch (Exception e) {
            System.out.println(e);
        }

    }

    /**
     * 获取分分彩计划
     */
    public String getFfc(){
    	String url = "http://774444.cc/json/ffc.json";
    	HttpGet getFfc = new HttpGet(url);
    	try {
			HttpResponse response = client.execute(getFfc);
			String entity = EntityUtils.toString(response.getEntity());
			Object gameMultiple = JSON.parseObject(entity).get("GameMultiple");
			String ffc = JSON.parseObject(gameMultiple.toString()).get("num").toString();
			System.out.println("分析号码:"+ffc);
			return ffc;
    	} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
    }
}
